package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.domain.User;
import cn.wolfcode.mapper.UserMapper;
import cn.wolfcode.query.UserQuery;
import cn.wolfcode.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* 用户信息服务接口实现
*/
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService  {

    @Override
    public IPage<User> queryPage(UserQuery qo) {
        IPage<User> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        QueryWrapper<User> wrapper = Wrappers.<User>query();
        return super.page(page, wrapper);
    }

    @Override
    public User login(String username, String password) {
        //判断user 对象是否存在,如果不存在,提示登录失败信息
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username).eq("password",password);
        User user = super.getOne(wrapper);
        if (user==null){
            throw new RuntimeException("账号或密码错误");
        }
        return user;
    }
}
